package org.mspring.mlog.freemarker.directive;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.mspring.mlog.BlogUtils;
import org.mspring.mlog.entity.em.ConfigGroup;
import org.mspring.mlog.service.ConfigService;
import org.mspring.nbee.web.freemarker.DirectiveUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * 站点标题
 *
 * @author Gao Youbo
 * @since 2015-02-11 18:31
 */
@Component
public class SiteTitleDirectiveModel implements TemplateDirectiveModel {
    private ConfigService configService;

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        String title = configService.getConfigValue(ConfigGroup.BASE, "sitename");
        Object temp = DirectiveUtils.getItem(env, BlogUtils.SITE_TITLE_KEY);
        if (temp != null) {
            title = temp.toString() + " - " + title;
        }
        env.getOut().append(title);
    }

    @Autowired
    public void setConfigService(ConfigService configService) {
        this.configService = configService;
    }
}
